// pages/error_book/index.js
import { IndexModel } from "../../models/index.js";
const indexModel = new IndexModel();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        errorCount:0,
        errorList:null,
        courseId:0,
        rightTimes:0,
        selRightTimes:0,
        settingDivShow:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
     let userInfo=indexModel._getUserData();
      const data= await indexModel.getErrorList(options.courseId);
      this.setData({
        courseId:options.courseId,
        errorCount:data.errorCount,
        errorList:data.errorList,
        rightTimes:userInfo.right_times,
        selRightTimes:userInfo.right_times,
      });
    },

    //练习全部错题
    testAll(event){
        if(this.data.errorCount>0){
            wx.navigateTo({
                url: `/pages/question/index?sourceType=4&typeId=0`
            });
        }
    },

    //练习某个章节
    testOneType(event){
        wx.navigateTo({
            url: `/pages/question/index?sourceType=4&typeId=${event.currentTarget.dataset.id}`
          });
    },
    //答对移除设置
    settingTimes(params){
        let type=params.currentTarget.dataset.type;
        let settingDivShow=true;
        if(type==1){
            settingDivShow=false;
        }
        this.setData({
            settingDivShow:settingDivShow
        });
    },
    selTimes(params){
        let value=params.currentTarget.dataset.value;
        this.setData({
            selRightTimes:value
        });
    },
    async settingTimeSave(){
        await indexModel.errorRemoveSetting(this.data.selRightTimes);
        this.setData({
            rightTimes:this.data.selRightTimes,
            settingDivShow:false
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    async onShow() {
        if(this.data.courseId!=0){
            let userInfo=indexModel._getUserData();
            const data= await indexModel.getErrorList(this.data.courseId);
            this.setData({
              errorCount:data.errorCount,
              errorList:data.errorList,
              rightTimes:userInfo.right_times,
              selRightTimes:userInfo.right_times,
            });
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})